#include <protocol.hpp>
//
#include <connector.hpp>
#include <flight_controller.hpp>
#include <flight_controller_tcp.hpp>
#include <flight_controller_tcp_textlog.hpp>
#include <flight_controller_udp_fdata.hpp>
#include <msg_command_s.hpp>
#include <msg_param_s.hpp>
#include <rtime.hpp>

/* @RegistMainFunction */
int fc_main(int argc, char** argv)
{
	if (argc == 2 && strcmp(argv[1], "tcp_start") == 0)
	{
		Connector::Instance()->Create(FC_TCP_PORT, &FlightControllerTcp::FlightControllerTcpCreator);
		Connector::Instance()->Create(FC_TCP_PORT_TEXTLOG, &FlightControllerTcpTextlog::FlightControllerTcpTextlogCreator);
		Connector::Instance()->Create(nullptr, FC_UDP_PORT_RECV_FDATA, new FlightControllerUdpFData());

		return 0;
	}
	else if (argc == 4 && strcmp(argv[1], "param_set") == 0)
	{
		msg_param_s param;
		strncpy(param.name, argv[2], 64);
		strncpy(param.value, argv[3], 64);
		Connector::Instance()->Send(FC_TCP_PORT, P_TYPE_PARAM_SET, (char*)&param, sizeof(msg_param_s));
		return 0;
	}
	else if (argc == 3 && strcmp(argv[1], "param_get") == 0)
	{
		msg_param_s param;
		strncpy(param.name, argv[2], 64);
		Connector::Instance()->Send(FC_TCP_PORT, P_TYPE_PARAM_GET, (char*)&param, sizeof(msg_param_s));
		return 0;
	}
	else if (argc == 2 && strcmp(argv[1], "param_load") == 0)
	{
		Connector::Instance()->Send(FC_TCP_PORT, P_TYPE_PARAM_LOAD, nullptr, 0);
		return 0;
	}
	else if (argc == 2 && strcmp(argv[1], "param_save") == 0)
	{
		Connector::Instance()->Send(FC_TCP_PORT, P_TYPE_PARAM_SAVE, nullptr, 0);
		return 0;
	}
	else if (argc == 4 && strcmp(argv[1], "command") == 0)
	{
		msg_command_s cmd;
		memset(&cmd, 0, sizeof(msg_command_s));
		cmd.command	   = strtoul(argv[2], NULL, 0);
		cmd.arg_u32[0] = strtol(argv[3], NULL, 0);
		Connector::Instance()->Send(FC_TCP_PORT, P_TYPE_COMMAND, (char*)&cmd, sizeof(msg_command_s));
		return 0;
	}

	LOGE("Command not found.\n");
	return 0;
}